package br.mywallet.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import br.mywallet.data.ContaDAO;
import br.mywallet.model.Conta;
import br.mywallet.model.Saldo;
import br.mywallet.util.Util;

import com.mywallet.R;

public class ContaIncluir extends Activity {
	
	private EditText editTextNome;
	private EditText editTextDataCriacao;
	private EditText editTextSaldoInicial;
	
	private Button buttonSalvar;
	private Button buttonCancelar;
	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conta_incluir);
        
        editTextNome = (EditText)findViewById(R.id.editTextContaNome);
        editTextDataCriacao = (EditText)findViewById(R.id.editTextContaDataCriacao);
        editTextSaldoInicial = (EditText)findViewById(R.id.editTextContaSaldoInicial);
        
        buttonSalvar = (Button)findViewById(R.id.buttonContaSalvar);
        buttonSalvar.setOnClickListener(
        		new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Conta conta = new Conta();
						conta.setDescricao(editTextNome.getText().toString());
						conta.setDataInclusao(Util.formataData(ContaIncluir.this,editTextDataCriacao.getText().toString()));
						conta.setSaldo(new Saldo(
											Util.formataData(ContaIncluir.this,editTextDataCriacao.getText().toString()),
											Float.parseFloat(editTextSaldoInicial.getText().toString())));
						
						(new ContaDAO()).persistirConta(conta,ContaIncluir.this);
						
						(Toast.makeText(ContaIncluir.this, "Conta cadastrada!", Toast.LENGTH_SHORT)).show();
						finish();
					}
				});
        
        buttonCancelar = (Button)findViewById(R.id.buttonContaCancelar);
		buttonCancelar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.conta_incluir, menu);
        return true;
    }
}
